import 'package:flutter/material.dart';
import 'package:wheel_of_fortune/utils/utils.dart';

class Counter extends StatefulWidget {
  final callBack;
  final int total;
  Counter({this.callBack, this.total, Key key}) : super(key: key);

  @override
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  TextEditingController controller;
  int count = 1;
  @override
  void initState() {
    super.initState();
    controller = TextEditingController(text: '${this.count}');
    controller.addListener(() {
      setState(() {
        if (controller.text.length == 0) {
          controller.text = '1';
        }

        if (widget.total != null && int.parse(controller.text) > widget.total) {
          controller.text = widget.total.toString();
        }
        count = int.parse(controller.text.replaceAll(' ', ''));
        if (widget.callBack != null) {
          widget.callBack(count);
        }
      });
    });
  }

  reduce() {
    this.count--;
    if (this.count == 0) {
      this.count++;
      return;
    }
    setState(() {
      controller.text = '${this.count}';
    });
  }

  add() {
    this.count++;
    if (widget.total != null && this.count > widget.total) {
      this.count--;
      return;
    }
    setState(() {
      controller.text = '${this.count}';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          InkWell(
            onTap: reduce,
            child: Container(
              width: 39,
              height: 39,
              decoration: BoxDecoration(
                color: this.count == 1 ? grey70Color : deepYellowColor,
                borderRadius: BorderRadius.circular(5),
              ),
              child: Center(
                child: Text(
                  '—',
                  style: TextStyle(
                    color: whiteColor,
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.only(left: 15, right: 15),
              width: 39,
              height: 39,
              decoration: BoxDecoration(
                color: Color(0xFFFFFFFF),
                borderRadius: BorderRadius.all(Radius.circular(6)),
                boxShadow: [
                  BoxShadow(
                    color: Color.fromARGB(255, 240, 240, 240),
                    offset: Offset(2.0, 2.0),
                    blurRadius: 10.0,
                    spreadRadius: 0.0,
                  ),
                  BoxShadow(color: Colors.white12, offset: Offset(1.0, 1.0)),
                  BoxShadow(color: Colors.white),
                ],
              ),
              child: TextField(
                enabled: false,
                controller: controller,
                //是否隐藏密码
                obscureText: false,
                textCapitalization: TextCapitalization.sentences,
                //控制光标样式
                cursorColor: deepYellowColor,
                cursorRadius: Radius.circular(6.0),
                cursorWidth: 1.0,
                //自动获取焦点
                autofocus: false,
                maxLines: 1,
                textAlign: TextAlign.center,
                //输入文本的样式
                style: TextStyle(
                  fontSize: 16.0,
                  color: deepYellowColor,
                  fontFamily: 'Avenir Next LT Pro',
                  fontWeight: FontWeight.bold,
                ),
                //在弹出键盘的时候修改键盘类型
                keyboardType: TextInputType.number,
                //键盘回车键的样式
                textInputAction: TextInputAction.send,
                decoration: InputDecoration(
                  hintText: '',
                  hintStyle: TextStyle(fontSize: 16, color: Color(0xFFB4B4B4)),
                  //填充颜色
                  fillColor: Colors.white,
                  filled: true,
                  // 边框的内边距
                  contentPadding: EdgeInsets.only(bottom: 7),
                  // suffix: Icon(data.iconData),
                  //边框
                  border: InputBorder.none,
                ),
              ),
            ),
          ),
          InkWell(
            // onLongPress: add,
            onTap: add,
            child: Container(
              width: 39,
              height: 39,
              decoration: BoxDecoration(
                color:
                    this.count == widget.total ? grey70Color : deepYellowColor,
                borderRadius: BorderRadius.circular(5),
              ),
              child: Icon(Icons.add, color: whiteColor),
            ),
          ),
        ],
      ),
    );
  }
}
